
"""
617. 合并二叉树
"""
from typing import Optional

from com.siyuan.TreeNode import TreeNode


class Solution:

    def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
        """
         * 分解二叉树，此题不能用遍历二叉树
         * 定义输入两棵树的根节点，返回一个合并后的二叉树根节点，思路，将第二个合并到第一个
        """
        if not root1:
            return root2
        if not root2:
            return root1

        root1.val += root2.val
        root1.left = self.mergeTrees(root1.left, root2.left)
        root1.right = self.mergeTrees(root1.right, root2.right)
        return root1
